package com.itbaizhan.service.impl;

import com.itbaizhan.dao.RecommentDao;
import com.itbaizhan.pojo.Item;
import com.itbaizhan.service.RecommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author spf
 * @date 2024/3/4
 * @time 14:21
 */
@Service
public class RecommentServiceImpl implements RecommentService {

    @Autowired
    private RecommentDao recommentDao;

    @Value("${livegoods.banner.nginx.prefix}")
    private String nginxPrefix;

    @Override
    public List<Item> getRecommendation(String city) {
        Query query = new Query();
        Criteria criteria = new Criteria();
        criteria.andOperator(Criteria.where("city").is(city),
                Criteria.where("recommendation").is(true));
        query.addCriteria(criteria);
        query.with(PageRequest.of(0,4, Sort.by(Sort.Direction.DESC, "recoSort")));

        List<Item> items = recommentDao.selectRecommendation(query);

        if (items.size() < 4){
            Query query1 = new Query();
            Criteria criteria1 = new Criteria();
            criteria1.andOperator(Criteria.where("city").ne(city),
                    Criteria.where("recommendation").is(true));
            query1.addCriteria(criteria1);
            query.with(PageRequest.of(0, 4 - items.size(), Sort.by(Sort.Direction.DESC, "recoSort")));

            List<Item> items1 = recommentDao.selectRecommendation(query1);

            items.addAll(items1);
        }

        // 托底数据
        if (items.size() < 4){
            for (int i = items.size(); i < 4; i++) {
                items.add(fallbackItem());
            }
        }

        // 修改图片路径
        items = changeImagesUrl(items);
        return items;
    }

    private List<Item> changeImagesUrl(List<Item> items) {
        for (Item item : items) {
            ArrayList<String> newImgs = new ArrayList<>();
            for (String img : item.getImgs()) {
                newImgs.add(nginxPrefix + img);
            }
            item.setImgs(newImgs);
        }

        return items;
    }

    // 托底数据
    private Item fallbackItem() {
        Item item = new Item();
        item.setId("5ec1ec6b7f56a946fb7fdffa");
        item.setCity("北京");
        item.setHouseType("150 ㎡");
        item.setImgs(
                Arrays.asList(
                        "group1/M00/00/01/wKhZjF6_UkuAGCsJABLGy04UWBI573.png",
                        "group1/M00/00/01/wKhZjF6_UlyANqRfAAjIoXS-cuE984.png",
                        "group1/M00/00/01/wKhZjF6_UmmAQsntAAro96E3Lio262.png"
                ));
        item.setPrice(12000L);
        item.setRecommendation(true);
        item.setRecoSort((byte)9);
        item.setRentType("整租");
        item.setSales(100L);
        item.setTitle("北京高档公寓");
        Map<String, String> info = new HashMap<>();
        info.put("years", "2010");
        info.put("type", "3室2厅");
        info.put("level", "10/18层");
        info.put("style", "精装修");
        info.put("orientation", "南北通透");
        item.setInfo(info);
        return item;
    }
}
